Skip to content

feat: integrate background_task system into agents#5

Merged
vtemian merged 14 commits intomainfrom
feature/background-task-integration
Jan 3, 2026
Merged

feat: integrate background_task system into agents#5
vtemian merged 14 commits intomainfrom
feature/background-task-integration

Conversation

@vtemian
Copy link
Owner

@vtemian vtemian commented Jan 1, 2026

Summary

  • Fix background_task system issues (memory leak, silent errors, missing types)
  • Integrate background_task into 3 agents for true async parallelism

Changes

Background Task System Fixes

  • Remove unused field: Removed progress.lastMessage (defined but never populated)
  • Add error logging: Replaced 4 silent .catch(() => {}) with proper console.error logging
  • Add task cleanup: TTL-based cleanup (1 hour) to prevent memory leak
  • Add response types: Proper TypeScript interfaces for session API responses
  • Add unit tests: 17 test cases covering all BackgroundTaskManager functionality

Agent Prompt Updates

Agent Pattern Benefit
project-initializer Fire-and-collect Run glob/read while 5 agents work in background
planner Fire-and-collect Run context7/btca_ask while 4 agents work
executor Fire-and-check Start reviewer immediately when its implementer finishes

Testing

  • All 40 tests passing
  • TypeScript compiles cleanly

Design Document

See thoughts/shared/designs/2026-01-01-background-task-integration-design.md (gitignored)


Summary by cubic

Integrated the background_task system into executor, planner, project-initializer, and brainstormer for real async parallelism, and fixed stability issues in the task manager. This speeds up execution and prevents memory leaks and silent failures.

  • New Features

    • Executor uses a fire-and-check pattern: fire implementers, poll, start reviewers immediately; includes fallback to Task.
    • Planner, project-initializer, and brainstormer use fire-and-collect/poll: fire background research, then collect results; includes fallback rules and auto-handoff to planner.
    • Added background tools usage in agent prompts: background_task, background_output, background_list.
    • Implementer commits after successful verification using the plan’s commit message.
  • Bug Fixes

    • Added 1h TTL cleanup to remove completed/error tasks and stop memory leak.
    • Replaced silent catch blocks with console.error logging.
    • Removed block option from background_output; use background_list polling, then background_output to avoid hangs.
    • Added TypeScript response types for session APIs; removed unused progress.lastMessage.
    • Added tests for manager behavior, error logging, cleanup, response typing, and agent prompt integration.

Written for commit ea234f7. Summary will update on new commits.

… unit tests

- Add error logging to replace silent catch blocks (Task 2)
- Add TTL-based cleanup to prevent memory leak (Task 3)
- Add proper response types for API calls (Task 4)
- Add comprehensive unit tests for BackgroundTaskManager (Task 5)
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 13 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="tests/tools/background-task-error-logging.test.ts">

<violation number="1" location="tests/tools/background-task-error-logging.test.ts:16">
P2: Test doesn&#39;t verify what its name claims. The assertion only checks that catch blocks have a named parameter, not that `console.error` is actually called. A catch like `.catch((err) =&gt; {})` would pass this test despite not logging anything. Consider parsing the full catch block body and verifying it contains `console.error`.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

…rage

- Add console.error to prompt session catch block
- Improve test to verify catch block bodies contain console.error
- Test now catches silent catch blocks like .catch((err) => {})
The blocking wait loop was checking a stale task object instead of
re-fetching the current status, causing infinite hangs when block=true.
- Remove block and timeout params from background_output tool
- Update executor, planner, project-initializer to use polling pattern
- Poll with background_list, then collect with background_output
- Simpler, more visible, no hanging
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 4 files (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="src/agents/project-initializer.ts">

<violation number="1" location="src/agents/project-initializer.ts:63">
P2: Polling instructions only check for &quot;completed&quot; status but don&#39;t account for tasks that may end in &quot;error&quot; state. This could cause the agent to poll indefinitely if any background task fails. Consider changing to: `Poll background_list until all tasks show &quot;completed&quot; or &quot;error&quot;`.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

…o-handoff to planner

- Use background_task instead of Task for parallel codebase research
- Fire-poll-collect pattern matches planner and executor
- Auto-spawn planner when user approves design (no extra confirmation)
- Add handoff phase for smooth transition to planning
- Commit after tests/types pass
- Use commit message from plan
- Stage only task-related files
- Do not push (executor handles that)
- Brainstormer: uses background_task, auto-handoff to planner
- Planner: library research tools (context7, btca_ask)
- Executor: fire-and-check pattern with polling
- Implementer: commits after verification
- Tools: add btca_ask, document fire-poll-collect pattern
Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1 issue found across 1 file (changes from recent commits).

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="README.md">

<violation number="1" location="README.md:268">
P3: Documentation inconsistency: This states &quot;All agents&quot; use fire-poll-collect, but the executor section above documents that executor uses a different &quot;fire-and-check&quot; pattern. Consider changing to &quot;Most agents&quot; or &quot;Research agents&quot; to be accurate.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

vtemian added a commit that referenced this pull request Jan 3, 2026
- Remove block parameter from background_output, use polling pattern
- Brainstormer: uses background_task for parallel research, auto-handoff to planner
- Planner: fire-poll-collect pattern for research
- Executor: fire-and-check pattern for parallel implementers/reviewers
- Implementer: commits after verification passes
- Project-initializer: fire-poll-collect for discovery
- Fix stale task status bug in blocking loop
- Add console.error to all catch blocks
- Update README with new patterns
- Poll until tasks show 'completed' or 'error' (not just completed)
- Skip errored tasks when collecting results
- Fix README: clarify which agents use which pattern
@vtemian vtemian merged commit 9efcad1 into main Jan 3, 2026
2 checks passed
@vtemian vtemian deleted the feature/background-task-integration branch January 3, 2026 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant